﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 05.06.2014
 * Time: 15:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data.Linq;



using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using Region.Utils;
namespace Region.Forms
{
  /// <summary>
  /// Description of EditAddress.
  /// </summary>
  public partial class EditAddress : Form
  {
    Address m_address;
    Engine m_engine;
    Boolean m_editlist=false;
    void ShowLists()
    {
      if (m_editlist) return;
      m_editlist=true;
      var address = (from t in m_engine.bd_FIO.Address select t);
		  //Region.BeginUpdate();
		  Region.Items.Clear();
		  String RegionTMP = Region.Text;
		  Region.Items.AddRange((from t in address group t.Region by t.Region into t2 where t2.Key!="" select t2.Key).ToArray());
		  Region.Text = RegionTMP;
		  //Region.EndUpdate();
		  if (Region.Text.Trim()!="")
		  {
		    address = from t in address where t.Region == Region.Text select t;
		    
		    String RaionTMP = Raion.Text;
		    Raion.Items.Clear();
		    Raion.Items.AddRange((from t in address group t.Raion by t.Raion into t2 where t2.Key!="" select t2.Key).ToArray());
		    Raion.Text = RaionTMP;
		    
		    if (Raion.Text.Trim()!="")
		      address = from t in address where t.Raion == Raion.Text select t;
		    var i = ( from t in address group t.City by t.City into t2 where t2.Key!="" select t2.Key);
		    
		    String CityTMP = City.Text;
		    City.Items.Clear();
		    City.Items.AddRange(( from t in address group t.City by t.City into t2 where t2.Key!="" select t2.Key).ToArray());
		    City.Text = CityTMP;

		    if (City.Text.Trim()!="")
		      address = from t in address where t.City == City.Text select t;
        
		    String SettlementTMP = Settlement.Text;
		    Settlement.Items.Clear();
		    Settlement.Items.AddRange(( from t in address group t.Settlement by t.Settlement into t2 where t2.Key!="" select t2.Key).ToArray());
        Settlement.Text = SettlementTMP;
        
		    if (Settlement.Text.Trim()!="")
		      address = from t in address where t.Settlement == Settlement.Text select t;
        
		    String StreetTMP = Street.Text;
		    Street.Items.Clear();
		    Street.Items.AddRange(( from t in address group t.Street by t.Street into t2 where t2.Key!="" select t2.Key).ToArray());
		    Street.Text = StreetTMP;
		  }
		  m_editlist = false;
    }
    public EditAddress(Engine engine, Address address)
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      m_address = address;
      m_engine =  engine;
      Region.Text = m_address.Region;
		  Raion.Text = m_address.Raion;
		  City.Text = m_address.City;
		  Settlement.Text = m_address.Settlement;
		  Street.Text = m_address.Street;
		  Home.Text = m_address.Home;
		  Structure.Text = m_address.Structure;
		  Room.Text = m_address.Room;
		  ShowLists();
		  //IEnumerable<Address> t2 = (from t in m_engine.bd_FIO.Address  select t);
    }

		void Button2Click(object sender, System.EventArgs e)
		{
		  Close();
		}
		void Button1Click(object sender, System.EventArgs e)
		{
		  m_address.Region = Region.Text;
		  m_address.Raion = Raion.Text;
		  m_address.City = City.Text;
		  m_address.Settlement = Settlement.Text;
		  m_address.Street = Street.Text;
		  m_address.Home = Home.Text;
		  m_address.Structure = Structure.Text;
		  m_address.Room = Room.Text;
      m_engine.bd_FIO.SubmitChanges();
		}
		void RegionSelectedIndexChanged(object sender, System.EventArgs e)
		{
		  ShowLists();
		}

  }
}
